//
//  DMBaseTableController.m
//  MCNKuaiYi
//
//  Created by 何典钊 on 2017/1/8.
//  Copyright © 2017年 Mr.Cannon. All rights reserved.
//

#import "DMBaseTableController.h"
#import "DMBaseViewCell.h"
#import "DMSettingCell.h"
#import "SZKCleanCache.h"
#import "DMDoctorModel.h"
#import "DMFeedbackViewController.h"

static NSString *Famous = @"Famous";
static NSString *setting = @"Setting";

@interface DMBaseTableController ()<UITableViewDataSource>
@property (nonatomic, strong) NSArray *tenPlist;
@property (nonatomic, strong) NSArray<DMDoctorModel *> *doctorArrM;
@property (nonatomic, strong) MJRefreshNormalHeader *refreshHeader;

@property(nonatomic,copy)NSString *cleanCache;
@end

@implementation DMBaseTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    //    _doctorArrM = [NSArray array];
    [self loadDoctordata];
    
    //计算缓存
    _cleanCache = [NSString stringWithFormat:@"清除缓存%.2fM",[SZKCleanCache folderSizeAtPath]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DMDoctorModel *model = _doctorArrM[indexPath.row];
    if ([_Identifier isEqualToString:@"设置与帮助"])
    {
        DMSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:setting forIndexPath:indexPath];
        cell.imgV.image = [UIImage imageNamed:_tenPlist[indexPath.row]];
        cell.illName.text = _tenPlist[indexPath.row];
        _tableView.rowHeight = 40;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        _tableView.tableFooterView = [[UIView alloc] init];
        return cell;
    }
    
    else
    {
        DMBaseViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Famous forIndexPath:indexPath];
        cell.model = model;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([_Identifier isEqualToString:@"设置与帮助"]) {
        
        return self.tenPlist.count;
    }
    return _doctorArrM.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([_Identifier isEqualToString:@"设置与帮助"]) {
        
        if (indexPath.row == 0 || indexPath.row == 1) {
            DMFeedbackViewController *feedBack = [[DMFeedbackViewController alloc] init];
            [self.navigationController pushViewController:feedBack animated:YES];
        }
        if (indexPath.row == 3) {
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:_cleanCache preferredStyle: UIAlertControllerStyleAlert];
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                [SZKCleanCache cleanCache:^{
                    [DMTools dmShowSVSuccessTitle:@"清除成功" andTime:1.0];
                }];
            }];
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }
}
//加载关注医生 和 名医数据
- (void)loadDoctordata {
    
    //1. 创建url
    //返回当前的时间戳
    BOOL isPullUp = YES;
    
    //如果下拉刷新正在招待
    isPullUp = !_refreshHeader.isRefreshing;
    
    NSInteger currentTime = [[NSDate date] timeIntervalSince1970];
    if (currentTime == 0) {
        currentTime = [[NSDate date] timeIntervalSince1970];
    }
    //定义
    NSString *URL = @"http://iosapi.itcast.cn/doctor/matchDoctors.json.php" ;
    NSDictionary *parameters = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089, @"page":@1,@"has_diagnosis":@2};;
    //解析
    [DMDoctorModel getDoctorListDataWithURLString:URL andParameters:parameters andSuccessBlock:^(NSArray *newsList) {
        //赋值
        _doctorArrM = newsList;
        
        //       NSLog(@"%@",newsList);
        //主线程刷新
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [_tableView reloadData];
            
            //如果是上拉
            if (isPullUp == NO) {
                [_refreshHeader endRefreshing];
                [DMTools dmShowSVSuccessTitle:@"刷新成功" andTime:1.0];
            }
        }];
    }];
}

- (void)setIdentifier:(NSString *)Identifier {
    _Identifier = Identifier;
    
    [_tableView reloadData];
}


#pragma mark - 搭建界面
- (void) setupUI {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    //设置代理
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = 75;
    [_tableView registerClass:[DMBaseViewCell class] forCellReuseIdentifier:Famous];
    [_tableView registerClass:[DMSettingCell class] forCellReuseIdentifier:setting];
    [self.view addSubview:_tableView];
    
    _refreshHeader = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadDoctordata)];
    
    //将上下拉刷新的控件添加到tableView上
    _tableView.mj_header = _refreshHeader;
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

//设置控制器的加载
- (NSArray *) testArr:(NSArray *)icoName {
    if (_tenPlist == nil) {
        NSMutableArray *arrM = [NSMutableArray array];
        [icoName enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [arrM addObject:obj];
        }];
        _tenPlist = arrM.copy;
    }
    return _tenPlist;
}

@end
